package com.software.authentication.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;

import com.software.common.CommonUtils;
import com.software.driver.dao.DriverTransaction;
import com.software.lineragent.dao.LinerAgentTransaction;
import com.software.motorcade.dao.MotorcadeTransaction;
import com.software.shipper.dao.ShipperTransaction;
import com.software.shipperagent.dao.ShipperAgentTransaction;

public class RegisterServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		String role = request.getParameter("role");
		String name = request.getParameter("name");
		String password = request.getParameter("password");
		String email = request.getParameter("email");
		String phone = request.getParameter("phone");
		String moto = request.getParameter("moto");
		String description = request.getParameter("description");
		
		if(role.equals("Shipper")){
			ShipperTransaction st = (ShipperTransaction) CommonUtils.getBean("shipperTransaction");
			boolean tag = st.register(name, password, email, phone);
			if(tag){
				request.getSession(true).setAttribute("user", email);
				request.getSession(true).setAttribute("role", role);
				try {
					CommonUtils.outputJsonData(1, response);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} else{
				try {
					CommonUtils.outputJsonData(0, response);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}else if(role.equals("ShippingAgent")){
			ShipperAgentTransaction sat = (ShipperAgentTransaction) CommonUtils.getBean("shipperAgentTransaction");
			boolean tag = sat.register(name, password, email, phone, description);
			if(tag){
				request.getSession(true).setAttribute("user", email);
				request.getSession(true).setAttribute("role", role);
				try {
					CommonUtils.outputJsonData(1, response);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} else{
				try {
					CommonUtils.outputJsonData(0, response);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}else if(role.equals("LinerAgent")){
			LinerAgentTransaction lat = (LinerAgentTransaction) CommonUtils.getBean("linerAgentTransaction");
			boolean tag = lat.register(name, password, email, phone, description);
			if(tag){
				request.getSession(true).setAttribute("user", email);
				request.getSession(true).setAttribute("role", role);
				try {
					CommonUtils.outputJsonData(1, response);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} else{
				try {
					CommonUtils.outputJsonData(0, response);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}else if(role.equals("Motorcade")){
			MotorcadeTransaction mt = (MotorcadeTransaction) CommonUtils.getBean("motorcadeTransaction");
			boolean tag = mt.register(name, password, email, phone, description);
			if(tag){
				request.getSession(true).setAttribute("user", email);
				request.getSession(true).setAttribute("role", role);
				try {
					CommonUtils.outputJsonData(1, response);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} else{
				try {
					CommonUtils.outputJsonData(0, response);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}else if(role.equals("Driver")){
			DriverTransaction dt = (DriverTransaction) CommonUtils.getBean("driverTransaction");
			boolean tag = dt.register(name, password, email, phone, moto);
			if(tag){
				request.getSession(true).setAttribute("user", email);
				request.getSession(true).setAttribute("role", role);
				request.getSession(true).setAttribute("drivername", dt.getName());
				try {
					CommonUtils.outputJsonData(1, response);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} else{
				try {
					CommonUtils.outputJsonData(0, response);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		else
		{
			try {
				CommonUtils.outputJsonData(0, response);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
